<?php


namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\user;

class LoginController extends Controller
{
    /**
     * 用户登录
     * @param string account
     * @param string password
     */
    public function login()
    {
        //获取用户信息
        $userData = user::getUserByAccountAndPassword(toString(requestData('account')),toString(requestData('password')));
        if(self::$client_id) $userData['client_id'] = self::$client_id;//绑定clientId
        //根据用户信息获取授权token
        $userData = self::setUserToken($userData);
        self::bindUid($userData['id']);//client_id绑定用户id
        self::bindUserData($userData);//client_id绑定用户信息
        $this->sendToClient(['code' => 200, 'f' => self::$f, 'data' => $userData]);
    }

    /**
     * 游客登录
     */
    public function customLogin()
    {
        //根据用户信息获取授权token
        $userData['id'] = 'custom';
        $userData = self::setUserToken($userData);
        self::bindUid($userData['id']);//client_id绑定用户id
        self::bindUserData($userData);//client_id绑定用户信息
        $this->sendToClient(['code' => 200, 'f' => self::$f, 'data' => $userData]);//发回给用户
    }
}
